package com.miao.teacher.utils;

import java.io.FileDescriptor;

import com.miao.teacher.app.TApp;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;

public class TMediaPlayer {
	private MediaPlayer mediaPlayer = null;

	public TMediaPlayer() {
		mediaPlayer = new MediaPlayer();
	}
	private String url;
	public String getUrl()
	{
		return url;
	}
	public void DoStop() {
		try {
			if (mediaPlayer != null && mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				mediaPlayer.release();
			}
		} catch (Exception er) {
		}
	}
	public void doPause()
	{
		if(mediaPlayer!=null)
		{
			mediaPlayer.pause();
		}
	}
	public int getLength(String url)
	{
		try{
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setDataSource(url);
			mediaPlayer.prepare();
		}catch(Exception er)
		{
			return 0;
		}
		return mediaPlayer.getDuration();
	}
	public void ASycPlay(final String mpath, final Handler mHandler) {
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					DoStop();
					mediaPlayer = new MediaPlayer();
					mediaPlayer
							.setOnCompletionListener(new OnCompletionListener() {
								@Override
								public void onCompletion(MediaPlayer mp) {
									// TODO Auto-generated method stub
									if(mHandler!=null){
									mHandler
											.obtainMessage(
													TApp.GOLDACTIVITY_PLAYFINISH)
											.sendToTarget();
									}
								}

							});
					mediaPlayer.setDataSource(mpath);
					mediaPlayer.prepare();
					mediaPlayer.start();
					url=mpath;
				} catch (Exception er) {
				}
			}

		}.start();
	}
	public void ASycPlay(final AssetFileDescriptor file, final Handler mHandler) {
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					DoStop();
					mediaPlayer = new MediaPlayer();
					mediaPlayer
							.setOnCompletionListener(new OnCompletionListener() {
								@Override
								public void onCompletion(MediaPlayer mp) {
									// TODO Auto-generated method stub
									if(mHandler!=null){
									mHandler
											.obtainMessage(
													TApp.GOLDACTIVITY_PLAYFINISH)
											.sendToTarget();
									}
								}

							});
					mediaPlayer.setDataSource(file.getFileDescriptor(),
							file.getStartOffset(),
							file.getLength());
					mediaPlayer.prepare();
					mediaPlayer.start();
				} catch (Exception er) {
					String mRes=er.toString();
					mRes=mRes+"";
				}
			}

		}.start();
	}
	public void ASycPlay(final AssetFileDescriptor file, final int mMark,final Handler mHandler) {
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					DoStop();
					mediaPlayer = new MediaPlayer();
					mediaPlayer
							.setOnCompletionListener(new OnCompletionListener() {
								@Override
								public void onCompletion(MediaPlayer mp) {
									// TODO Auto-generated method stub
									if(mHandler!=null){
									mHandler
											.obtainMessage(
													mMark)
											.sendToTarget();
									}
								}

							});
					mediaPlayer.setDataSource(file.getFileDescriptor(),
							file.getStartOffset(),
							file.getLength());
					mediaPlayer.prepare();
					mediaPlayer.start();
				} catch (Exception er) {
					String mRes=er.toString();
					mRes=mRes+"";
				}
			}

		}.start();
	}
	public void ASycPlay(final String mpath, final int mMark,final Handler mHandler) {
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					DoStop();
					mediaPlayer = new MediaPlayer();
					mediaPlayer
							.setOnCompletionListener(new OnCompletionListener() {
								@Override
								public void onCompletion(MediaPlayer mp) {
									// TODO Auto-generated method stub
									if(mHandler!=null){
									mHandler
											.obtainMessage(
													mMark)
											.sendToTarget();
									}
								}

							});
					mediaPlayer.setDataSource(mpath);
					mediaPlayer.prepare();
					mediaPlayer.start();
				} catch (Exception er) {
				}
			}

		}.start();
	}
}
